<template>
  <div class="operation-log-container page-container">
    <div class="page-header">
      <h2>操作日志</h2>
    </div>
    
    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="操作类型">
          <el-select v-model="queryParams.actionType" placeholder="选择操作类型" clearable>
            <el-option label="添加" value="add" />
            <el-option label="修改" value="update" />
            <el-option label="删除" value="delete" />
            <el-option label="查询" value="query" />
            <el-option label="导出" value="export" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="操作人ID">
          <el-input v-model="queryParams.operatorId" placeholder="请输入操作人ID" clearable />
        </el-form-item>
        <el-form-item>
          <div class="button-group">
            <el-button type="primary" @click="handleQuery">
              <el-icon><Search /></el-icon> 搜索
            </el-button>
            <el-button @click="resetQuery">
              <el-icon><Refresh /></el-icon> 重置
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    
    <!-- 日志列表 -->
    <div class="table-card">
      <el-table
        v-loading="loading"
        :data="logList"
        border
        style="width: 100%"
        :stripe="true"
      >
        <el-table-column prop="id" label="日志ID" width="80" align="center" />
        <el-table-column prop="operatorId" label="操作人ID" width="100" align="center" />
        <el-table-column prop="operatorName" label="操作人名称" width="120" align="center" />
        <el-table-column label="操作类型" width="100" align="center">
          <template #default="scope">
            <el-tag :type="getActionTypeTag(scope.row.actionType)">{{ formatActionType(scope.row.actionType) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="操作描述" min-width="250" show-overflow-tooltip />
        <el-table-column prop="targetType" label="操作对象" width="100" align="center" />
        <el-table-column prop="targetId" label="对象ID" width="100" align="center" />
        <el-table-column prop="operationTime" label="操作时间" width="180" align="center" />
        <el-table-column prop="ipAddress" label="操作IP" width="150" align="center" />
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.pageNum"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'
import { getOperationLogs } from '@/api/log'

// 查询条件
const queryParams = reactive({
  actionType: '',
  operatorId: ''
})

// 分页参数
const pagination = reactive({
  pageNum: 1,
  pageSize: 10,
})

// 列表数据
const logList = ref([])
const total = ref(0)
const loading = ref(false)

// 操作类型映射
const actionTypeMap = {
  add: '添加',
  update: '修改',
  delete: '删除',
  query: '查询',
  export: '导出',
  other: '其他'
}

const actionTypeTagMap = {
  add: 'success',
  update: 'warning',
  delete: 'danger',
  query: 'info',
  export: 'primary',
  other: 'info'
}

// 格式化操作类型
const formatActionType = (type) => {
  return actionTypeMap[type?.toLowerCase()] || type
}

// 获取操作类型标签
const getActionTypeTag = (type) => {
  return actionTypeTagMap[type?.toLowerCase()] || 'info'
}

// 获取操作日志列表
const fetchOperationLogs = async () => {
  loading.value = true
  try {
    const params = {
      ...queryParams,
      ...pagination
    }
    // 移除空值参数
    Object.keys(params).forEach(key => {
      if (params[key] === '' || params[key] === null || params[key] === undefined) {
        delete params[key]
      }
    })
    const res = await getOperationLogs(params)
    if (res.code === 200) {
      logList.value = res.data.records || res.data.list || []
      total.value = res.data.total || 0
    } else {
      ElMessage.error(res.msg || '获取操作日志失败')
    }
  } catch (error) {
    console.error('获取操作日志异常:', error)
    ElMessage.error('获取操作日志失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleQuery = () => {
  pagination.pageNum = 1
  fetchOperationLogs()
}

// 重置
const resetQuery = () => {
  queryParams.actionType = ''
  queryParams.operatorId = ''
  handleQuery()
}

// 分页大小改变
const handleSizeChange = (size) => {
  pagination.pageSize = size
  fetchOperationLogs()
}

// 当前页改变
const handleCurrentChange = (page) => {
  pagination.pageNum = page
  fetchOperationLogs()
}

// 页面加载时获取数据
onMounted(() => {
  fetchOperationLogs()
})
</script>

<style scoped>
/* 沿用全局样式和布局样式 */
</style> 